<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Articles;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class ArticleController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }


    /**
     * 查看
     */
    public function getArticleAction()
    {
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $id = $this->params['id'];
        } else {
            $this->fail(-1, '营销id不能为空');
        }
        $article        = new Articles();
        $list           = $article->find_first_by_Id($id);
        $list['images'] = explode(',', $list['images']);
        $this->ok($list);
    }


    /**
     * 编辑
     */
    public function updateArticleAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '营销id不能为空');
        }
        $this->validate_params($this->params);
        $article                              = new Articles();
        $first_article                        = $article->find_first_by_Id($this->params['id']);
        $first_article['updated_at']          = date('Y-m-d H:i:s', time());
        $first_article['title']               = $this->params['title'];
        $first_article['article_category_id'] = $this->params['article_category_id'];
        $first_article['content']             = $this->params['content'];
        $first_article['author']              = $this->params['author'];
        $first_article['sort']                = $this->params['sort'];
        $first_article['nickname']            = $this->params['author'];
        $first_article['avatar']              = $this->params['avatar'];
        $first_article['images']              = implode(',', $this->params['images']);
        $first_article['share_num']           = $this->params['share_num'];
        $result                               = $article->save_article($first_article);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    public function addArticleAction()
    {
        unset($this->params['_url']);
        $this->params['created_at'] = date('Y-m-d H:i:s', time());
        $this->validate_params($this->params);
        $article                  = new Articles();
        $this->params['images']   = implode(',', $this->params['images']);
        $this->params['nickname'] = $this->params['author'];
        $result                   = $article->save_article($this->params);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '新增失败');
        }
    }


    private function validate_params($params)
    {
        if (empty($params['images'])) {
            $this->fail(-1, '图片必须上传');
        }
        if (empty($params['title'])) {
            $this->fail(-1, '营销标题不能为空');
        }
        if (empty($params['article_category_id'])) {
            $this->fail(-1, '营销分类不能为空');
        }
        if (empty($params['content'])) {
            $this->fail(-1, '营销内容不能为空');
        }
        if (empty($params['author'])) {
            $this->fail(-1, '作者不能为空');
        }
    }

    /**
     * 删除
     */
    public function deleteArticleAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '营销id不能为空');
        }
        $article    = new Articles();
        $find_first = $article->findFirstById($this->params['id']);
        $result     = $find_first->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }


    /**
     * 获取token
     */
    public function gettokenAction()
    {
        $token = $this->qiniu->get_token();
        $this->ok(['token' => $token]);
    }


    public function oneyuanlistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $freegoods = new Freegoods();
        $list      = $freegoods->goods_list($this->params);

        $paginator    = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items   = $paginator->getPaginate();
        $list_items   = CommonController::transition_array($list_items);
        $current_time = time();
        foreach ($list_items['items'] as $key => $value) {
            $send_start_time = strtotime($value['send_start_time']);
            $send_end_time   = strtotime($value['send_end_time']);
            if ($send_start_time <= $current_time && $send_end_time >= $current_time) {
                $list_items['items'][$key]['status'] = 1;
            } else {
                $list_items['items'][$key]['status'] = 0;
            }
        }
        $f_order_commissions = new OrderCommissions();
        $list_items['month_count'] = $f_order_commissions->monthZero();
        $this->ok($list_items);
    }

    /**
     * 删除一元购
     */
    public function delOneyuanAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少id');
        }

        $ids = explode(',',$this->params['id']);
        $freegoods = new Freegoods();
        $flag = true;
        foreach ($ids as $key=>$value) {
            $freegoodsInfo = $freegoods->findFirstById($value);
            if (!$freegoodsInfo) {
                $flag = false;
            }
            if (!$freegoodsInfo->delete()) {
                $flag = false;
            }
        }
        if($flag){
            $this->ok([]);
        }else{
            $this->fail(-1,'删除失败');
        }
    }


    /**
     * 修改分类
     */
    public function updateCateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '分类id不能为空');
        }
        $cateArticle              = new ArticleCategories();
        $find_frist               = $cateArticle->find_first_by_Id($this->params['id']);
        $find_frist['name']       = $this->params['name'];
        $find_frist['sort']       = $this->params['sort'];
        $find_frist['type']       = $this->params['type'];
        $find_frist['updated_at'] = date('Y-m-d H:i:s', time());
        $result                   = $cateArticle->save_cate($find_frist);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }

    }


    /**
     * 添加分类
     */
    public function addCateAction()
    {
        $cateArticle              = new ArticleCategories();
        $find_frist['name']       = $this->params['name'];
        $find_frist['sort']       = $this->params['sort'];
        $find_frist['type']       = $this->params['type'];
        $find_frist['created_at'] = date('Y-m-d H:i:s', time());
        $result                   = $cateArticle->save_cate($find_frist);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }

    public function deleteCateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '分类id不能为空');
        }
        $article       = new Articles();
        $article_count = $article->article_count($this->params['id']);
        if ($article_count == 0) {
            $cate_article = new ArticleCategories();
            $result       = $cate_article->findFirstById($this->params['id'])->delete();
            if ($result) {
                $this->ok([]);
            } else {
                $this->fail(-1, '删除失败');
            }
        } else {
            $this->fail(-1, '下面有营销素材，不能删除，请先删除营销素材');
        }


    }

}
